package com.zj.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.zj.config.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayERServiceImpl implements AlipayERService{
    @Autowired
    private AlipayConfig alipayConfig;

    @Override
    public String createQRCodeOrder(Long outTradeNo, String totalAmount, String email) {
        try {
            // 1. 创建支付宝客户端实例
            AlipayClient alipayClient = new DefaultAlipayClient(
                    alipayConfig.getGatewayUrl(),
                    alipayConfig.getAppId(),
                    alipayConfig.getMerchantPrivateKey(),
                    alipayConfig.getFormat(),
                    alipayConfig.getCharset(),
                    alipayConfig.getAlipayPublicKey(),
                    alipayConfig.getSignType()
            );

            // 2. 创建二维码支付请求对象
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            request.setReturnUrl(alipayConfig.getReturnUrl());
            request.setNotifyUrl(alipayConfig.getNotifyUrl());

            // 3. 构建业务参数
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", outTradeNo);
            bizContent.put("total_amount", totalAmount);
            bizContent.put("subject", "是甜蜜屋里面的商品");
            // 二维码支付使用不同的产品码
            bizContent.put("product_code", "FACE_TO_FACE_PAYMENT");

            request.setBizContent(bizContent.toString());

            // 4. 执行请求并获取响应
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                // 返回二维码内容（二维码图片的URL或二维码字符串）
                return response.getQrCode();
            } else {
                throw new RuntimeException("创建二维码支付订单失败: " + response.getSubMsg());
            }
        } catch (AlipayApiException e) {
            throw new RuntimeException("支付宝接口调用异常", e);
        }
    }

    // 查询订单支付状态
    @Override
    public String queryOrderStatus(Long outTradeNo) {
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(
                    alipayConfig.getGatewayUrl(),
                    alipayConfig.getAppId(),
                    alipayConfig.getMerchantPrivateKey(),
                    alipayConfig.getFormat(),
                    alipayConfig.getCharset(),
                    alipayConfig.getAlipayPublicKey(),
                    alipayConfig.getSignType()
            );

            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            JSONObject bizContent = new JSONObject();
            bizContent.put("out_trade_no", outTradeNo);
            request.setBizContent(bizContent.toString());

            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                return response.getTradeStatus();
            } else {
                return "QUERY_FAILED";
            }
        } catch (AlipayApiException e) {
            throw new RuntimeException("查询订单状态异常", e);
        }
    }
}
